package com.ganbo.ms.java8.stream;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.val;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ganbo
 * @date 2022/12/18 10:18
 */
public class Demo3 {

    public static void main(String[] args) {

        List<Student> list = new ArrayList<>();
        list.add(new Student("赵丽颖", 58, 95));
        list.add(new Student("杨颖", 56, 88));
        list.add(new Student("迪丽热巴", 56, 99));
        list.add(new Student("柳岩", 52, 50));

        //获取最大值
        final val collect = list.stream().collect(Collectors.maxBy(((o1, o2) -> o1.getSocre() - o2.getSocre())));
        final Student student = collect.get();
        System.out.println(student);

        //获取最小值
        final Student student1 = list.stream().collect(Collectors.minBy(((o1, o2) -> o1.getSocre() - o2.getSocre()))).get();
        System.out.println(student1);
        //求总和
        final val collect1 = list.stream().collect(Collectors.summingInt(Student::getSocre));
        System.out.println(collect1);

        //根据年龄分组
        final Map<Integer, List<Student>> map = list.stream().collect(Collectors.groupingBy(Student::getAge));
        map.forEach((k, v) -> {
            System.out.println(k + "::" + v);
        });

        final Map<String, List<Student>> collect2 = list.stream().collect(Collectors.groupingBy(o -> {
            if (o.getSocre() > 60) return "及格";
            else return "不及格";
        }));

        System.out.println("==============");

        collect2.forEach((k, v) -> {
            System.out.println(k + "::" + v);
        });


    }
}


@Data
@AllArgsConstructor
class Student {
    private String name;
    private int age;
    private int socre;
}
